package com.oigx.lookmu;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import com.intellij.openapi.wm.StatusBarWidgetFactory;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.util.Consumer;
import com.oigx.lookmu.core.SettingsState;
import com.oigx.lookmu.core.ShowLineUtil;
import com.oigx.lookmu.core.TxtBook;
import com.oigx.lookmu.core.TxtChapter;
import com.oigx.lookmu.core.TxtReaderUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.event.MouseEvent;

public class LookMiniFactory implements StatusBarWidgetFactory {

    public static final String ID = "LookMini";

    @Override
    public @NonNls @NotNull String getId() {
        return ID;
    }

    @Override
    public @Nls @NotNull String getDisplayName() {
        return "LookMuMi";
    }

    @Override
    public boolean isAvailable(@NotNull Project project) {
        return true;
    }

    @Override
    public @NotNull StatusBarWidget createWidget(@NotNull Project project) {
        return new LookMiniWidget(project);
    }

    @Override
    public void disposeWidget(@NotNull StatusBarWidget widget) {
        widget.dispose();
    }

    @Override
    public boolean canBeEnabledOn(@NotNull StatusBar statusBar) {
        return false;
    }



    public static class LookMiniWidget implements StatusBarWidget, StatusBarWidget.TextPresentation {

        private static final String ID = "LookMiniWg";
        private final Project project;

        public LookMiniWidget(Project project) {
            this.project = project;
        }

        @Override
        public @NonNls @NotNull String ID() {
            return ID;
        }


        @Override
        public @Nullable WidgetPresentation getPresentation() {
            return new LookMiniWidget(project);
        }

        @Override
        public void install(@NotNull StatusBar statusBar) {
        }

        @Override
        public void dispose() {

        }

        @Override
        public @Nullable @NlsContexts.Tooltip String getTooltipText() {
            return "look mu mini bar";
        }

        @Override
        public @Nullable Consumer<MouseEvent> getClickConsumer() {
            System.out.println(project.getName());
            return e->{
                System.out.println("click ->" + e.getClickCount());
                if (e.getClickCount()==1){
                    ShowLineUtil.nextLine(project);
                } else if (e.getClickCount()==2){
                    StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
                    if (statusBar != null){
                        statusBar.setInfo("");
                    }
                } else if (e.getClickCount()==3){
                    ShowLineUtil.prevLine(project);
                }
            };
        }



        @Override
        public @NotNull @NlsContexts.Label String getText() {
            return "->>";
        }

        @Override
        public float getAlignment() {
            return 0;
        }
    }



}
